package com.ruipengkj.component.jtable;

import java.awt.Color;
import java.awt.Component;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.border.Border;
import javax.swing.table.DefaultTableCellRenderer;

import com.ruipengkj.commons.util.Util;

public class RPTableCellRenderer extends DefaultTableCellRenderer {

	private static final long serialVersionUID = 1L;
	private Color backgroundEven = new Color(247, 251, 254);
    private Color backgroundOdd = new Color(226, 238, 250);
    private Color backgroundSelected = new Color(255, 223, 156);
    private Color selectedTextColor = Color.BLACK;
    private Color textColor = Util.TABLE_TEXT_COLOR;
    private Border border = BorderFactory.createEmptyBorder(0, Util.TABLE_CELL_LEADING_SPACE, 0, 0);
    
    @Override
    public Component getTableCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int row, int column) {
        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        
        this.setFont(Util.TABLE_CELL_FONT);
        this.setBorder(border);
        
        if (value != null && value instanceof Boolean) {
        	JCheckBox cb = new JCheckBox("", (Boolean)value);
        	cb.setOpaque(false);
        	JPanel jp = new JPanel();
        	jp.add(cb);
        	if (!isSelected) {
                if (row % 2 == 1) {
                	jp.setBackground(backgroundOdd);
                } else {
                	jp.setBackground(backgroundEven);
                }
                jp.setForeground(textColor);
            } else {
            	jp.setBackground(backgroundSelected);
            	jp.setForeground(selectedTextColor);
            }
        	return jp;
        } else {
        	if (!isSelected) {
                if (row % 2 == 1) {
                    this.setBackground(backgroundOdd);
                } else {
                    this.setBackground(backgroundEven);
                }
                this.setForeground(textColor);
            } else {
                this.setBackground(backgroundSelected);
                this.setForeground(selectedTextColor);
            }
        }
        return this;
    }
}
